import pygame
import time
import math
from constant import *

class Car(pygame.sprite.Sprite):
    '''初始化'''
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(CAR_IMAGE) # 加载车辆
        self.rect = self.image.get_rect()
        self.rect.centerx = 0
        self.rect.centery = 0

    '''巡线'''
    def run(self, line_points):
        while True:
            for index in range(0, len(line_points)-1):
                start_point = line_points[index] # 起点
                end_point = line_points[index+1] # 终点

                self.rect.centerx = start_point[0]
                self.rect.centery = start_point[1]

                if start_point[0] == end_point[0]: # 竖直运动
                    self.rect.centerx = end_point[0]
                    if end_point[1] - start_point[1] > 0:
                        while self.rect.centery < end_point[1]:
                            self.rect.centery = self.rect.centery + CAR_SPEED*CAR_TIME
                            time.sleep(CAR_TIME)
                    else:
                        while self.rect.centery > end_point[1]:
                            self.rect.centery = self.rect.centery - CAR_SPEED*CAR_TIME
                            time.sleep(CAR_TIME)

                elif start_point[1] == end_point[1]: # 水平运动
                    self.rect.centery = end_point[1]
                    if end_point[0] - start_point[0] > 0:
                        while self.rect.centerx < end_point[0]:
                            self.rect.centerx = self.rect.centerx + CAR_SPEED*CAR_TIME
                            time.sleep(CAR_TIME)
                    else:
                        while self.rect.centerx > end_point[0]:
                            self.rect.centerx = self.rect.centerx - CAR_SPEED*CAR_TIME
                            time.sleep(CAR_TIME)

                else: # 斜线运动
                    ratio = (end_point[1]-start_point[1])/(end_point[0]-start_point[0])
                    if end_point[0] - start_point[0] > 0 and end_point[1] - start_point[1] > 0: # 向右下运动
                        while self.rect.centerx < end_point[0] and self.rect.centery < end_point[1]:
                            self.rect.centerx = self.rect.centerx + CAR_SPEED*CAR_TIME*abs(math.cos(math.atan(ratio)))
                            self.rect.centery = self.rect.centery + CAR_SPEED*CAR_TIME*abs(math.sin(math.atan(ratio)))
                            time.sleep(CAR_TIME)
                    elif end_point[0] - start_point[0] > 0 and end_point[1] - start_point[1] < 0: # 向右上运动
                        while self.rect.centerx < end_point[0] and self.rect.centery > end_point[1]:
                            self.rect.centerx = self.rect.centerx + CAR_SPEED*CAR_TIME*abs(math.cos(math.atan(ratio)))
                            self.rect.centery = self.rect.centery - CAR_SPEED*CAR_TIME*abs(math.sin(math.atan(ratio)))
                            time.sleep(CAR_TIME)
                    elif end_point[0] - start_point[0] < 0 and end_point[1] - start_point[1] > 0: # 向左下运动
                        while self.rect.centerx > end_point[0]  and self.rect.centery < end_point[1]:
                            self.rect.centerx = self.rect.centerx - CAR_SPEED*CAR_TIME*abs(math.cos(math.atan(ratio)))
                            self.rect.centery = self.rect.centery + CAR_SPEED*CAR_TIME*abs(math.sin(math.atan(ratio)))
                            time.sleep(CAR_TIME)
                    elif end_point[0] - start_point[0] < 0 and end_point[1] - start_point[1] < 0:# 向左上运动
                        while self.rect.centerx > end_point[0]  and self.rect.centery > end_point[1]:
                            self.rect.centerx = self.rect.centerx - CAR_SPEED*CAR_TIME*abs(math.cos(math.atan(ratio)))
                            self.rect.centery = self.rect.centery - CAR_SPEED*CAR_TIME*abs(math.sin(math.atan(ratio)))
                            time.sleep(CAR_TIME)
